﻿using AutoMapper;
using HealthcareSystem.API.Write.Application.Command.ChronicDisease;
using HealthcareSystem.Domain;
using HealthcareSystem.Domain.ChronicDiseaseManagement;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel.ChronicDisease
{
    public class HealthReportAddCommandHandler : IRequestHandler<HealthReportAddCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<HealthReport> heRepisitory;
        private readonly IMapper mapper;

        public HealthReportAddCommandHandler(IBaseRepository<HealthReport> hrRepository, IMapper mapper)
        {
            this.heRepisitory = hrRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(HealthReportAddCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ResultCode.添加成功;
            result.Message = ResultCode.添加成功.ToString();

            HealthReport he = mapper.Map<HealthReport>(request);
            
            result.Data = await heRepisitory.AddAsync(he);
            return await Task.FromResult(result);
        }
    }
}
